module tcs {
	export class index extends eui.Component implements eui.UIComponent{
		private bg:eui.Rect;
		private up:eui.Rect;
		private down:eui.Rect;
		private right1:eui.Rect;
		private left1:eui.Rect;
		private btnjs:eui.Button;
		private head:tcs.body;
		private head1:tcs.body;
		private body:tcs.body;
		private btnl:eui.Button;
		private btnr:eui.Button;
		private btnu:eui.Button;
		private btnd:eui.Button;
		//private btnjs:egret.Bitmap;
		private btnstop:egret.Bitmap;
		private btnstr:egret.Bitmap;
		private main:Main;
		private fx:number;
		private stopnm:number=0;
		private movefx:number;
		private snake:tcs.body[];
		private headx:number;
		private heady:number;
		private timer:egret.Timer=new egret.Timer(100);
		private scrol:number=0;
		private showscrol:egret.TextField;
		private movespeed:number=10000;
		private scrolban:tcs.scrolpanel;
		private touchpointx:number;
		private touchpointy:number;
		public constructor() {
			super();
			this.main=new Main;
			//this.body=new tcs.body();
			this.head=tcs.body.bodyadd("head_png");
			this.head1=tcs.body.bodyadd("body_png");
			//this.btnjs=this.main.createBitmapByName("js_png");
			//this.btnjs.x=1000;
			//this.btnjs.y=510;
			//this.btnjs.touchEnabled=true;
			this.head1.x=310;
			this.head1.y=560;
			this.btnstop=this.main.createBitmapByName("stop_png");
			this.btnstop.x=900;
			this.btnstop.y=510;
			this.btnstop.touchEnabled=true;
			this.btnstr=this.main.createBitmapByName("start_png");
			this.btnstr.x=900;
			this.btnstr.y=510;
			this.btnstr.touchEnabled=true;
			//this.body=this.main.createBitmapByName("body_png");
			this.snake=[];
			this.snake.push(this.head);
			this.snake.push(this.head1);
            this.showscrol=new egret.TextField();
			this.showscrol.width=150;
			this.showscrol.x=500;
			this.showscrol.y=15;
			this.showscrol.size=20;
			this.showscrol.text="得分：00"
			
			
			
			
		}

		protected childrenCreated():void
		{    
			//console.log
			this.timer.start();
			this.timer.addEventListener(egret.TimerEvent.TIMER,this.headmove,this);
			this.addChild(this.showscrol)

			this.createanj();
			
			this.createhead();
			this.bodyadd();
			this.addChild(this.btnjs);
			
            this.btnjs.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.touch,this);
			this.btnjs.addEventListener(egret.TouchEvent.TOUCH_END,this.touch,this);
			this.btnstopadd();
			this.btnstop.addEventListener(egret.TouchEvent.TOUCH_TAP,this.stop,this);
			this.btnstr.addEventListener(egret.TouchEvent.TOUCH_TAP,this.stop,this);
			//console.log("头的所在的层"+this.getChildIndex(this.head))
		}
		private touch(e:egret.Event){
           if(e.type==egret.TouchEvent.TOUCH_BEGIN){
                 this.addEventListener(egret.Event.ENTER_FRAME,this.js,this);
		   }else{console.log("移除了加速的帧监听事件");
				this.removeEventListener(egret.Event.ENTER_FRAME,this.js,this);
			}
		}
		private btnstopadd(){
			this.btnstr.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.stop,this);
			this.addChild(this.btnstop);
			this.btnstop.addEventListener(egret.TouchEvent.TOUCH_TAP,this.stop,this);
		}
		private btnstradd(){
			this.btnstop.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.stop,this);
			this.addChild(this.btnstr);
			this.btnstr.addEventListener(egret.TouchEvent.TOUCH_TAP,this.stop,this);
		}
		private touchpoint(e:egret.TouchEvent){
            this.touchpointx=e.localX;
			this.touchpointy=e.localY;
		}
		private js(e:egret.TouchEvent){

			//朝当前方向加速移动
		 	if(this.movefx==1){this.snake[0].y-=5;}
			else if(this.movefx==2){this.snake[0].x-=5;}
			else if(this.movefx==3){this.snake[0].y+=5;}
			else if(this.movefx==4){this.snake[0].x+=5;}
           //身体跟着头部运动
			for(let i=this.snake.length-1;i>0;i--){
			this.snake[i].x=this.snake[i-1].x;
			this.snake[i].y=this.snake[i-1].y;
			console.log("调用了加速的帧监听事件");
			
			}
			
		}
		private stop(){
		 this.stopnm+=1;
		 console.log("第"+this.stopnm+"次单击暂停/开始按钮")
		if(this.stopnm%2==0){
			this.removeChild(this.btnstr);
			this.btnstopadd();
			this.timer.reset();this.timer.start();
			console.log("重启计时器")
		}else{
			this.removeChild(this.btnstop);
			this.btnstradd();
			this.timer.stop();
			console.log("停止计时器")
		 }
		}
		private bodyadd(){
			let a=Math.ceil(Math.random()*1090);
			let b=Math.ceil(Math.random()*600);
			if(a>=10&&b>=10){
				this.body=tcs.body.bodyadd("body_png");
				this.body.x=a;
				this.body.y=b;
				this.addChildAt(this.body,this.numChildren-1);
				
				console.log("身体的x和y坐标在规定范围内，创建身体")
				}else{this.bodyadd();
				console.log("身体坐标不在范围内，重新随机")}
}
		private createhead(){
			this.headx=this.head.x=310;
			this.heady=this.head.y=560;
			this.addChildAt(this.head,5)
			console.log("创建头部，位于第"+this.getChildIndex(this.head)+"层")
		}
		private headmove(e:egret.TouchEvent){
	
			if(this.fx==1){
				
				this.snake[0].y-=5;this.movefx=1;}
			else if(this.fx==3){this.snake[0].y+=5;this.movefx=3;}
			else if(this.fx==2){this.snake[0].x-=5;this.movefx=2;}
			else if(this.fx==4){this.snake[0].x+=5;this.movefx=4;}

			for(let i=this.snake.length-1;i>0;i--){
			this.snake[i].x=this.snake[i-1].x;
			this.snake[i].y=this.snake[i-1].y;
		}
			
			
		 this.hittest(this.head,this.body);
		this.hittestbian(this.head,this.up);
		this.hittestbian(this.head,this.left1);
		this.hittestbian(this.head,this.down);
		this.hittestbian(this.head,this.right1);

			if(e.target==this.btnu){
				this.fx=1
			}else if(e.target==this.btnr){
				this.fx=4
			}else if(e.target==this.btnl){
				this.fx=2
			}else if(e.target==this.btnd){
				this.fx=3
			}

		 
		}
        private hittest(obj1:egret.DisplayObject,obj2:egret.DisplayObject){
		var rect1:egret.Rectangle=obj1.getBounds();
		var rect2:egret.Rectangle=obj2.getBounds();
		rect1.x=obj1.x
		rect1.y=obj1.y
		rect2.x=obj2.x
		rect2.y=obj2.y
		if(rect1.intersects(rect2)){
			this.scrol+=1;
			this.showscrol.text="得分："+this.scrol;
			this.body.x=this.snake[length].x
			this.body.y=this.snake[length].y
			this.snake.push(this.body)
			console.log("头和身体发生了碰撞，将该身体加入到snake数组中，并再次调用bodyadd方法创建新的身体")	
			
			this.bodyadd();}
		
		}
		private hittestbian(obj1:egret.DisplayObject,obj2:egret.DisplayObject){
		let rect1:egret.Rectangle=obj1.getBounds();
		let rect2:egret.Rectangle=obj2.getBounds();
		rect1.x=obj1.x
		rect1.y=obj1.y
		rect2.x=obj2.x
		rect2.y=obj2.y
		if(rect1.intersects(rect2)){
			this.removeEventListener(egret.Event.ENTER_FRAME,this.js,this);
			this.timer.stop();
			this.gameover();
			console.log("头和四周发生了碰撞，停止计时器，调用gameover方法")
		}}
		private gameover(){
		    
			this.snake=[];
			this.btnd.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.headmove,this);
			this.btnl.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.headmove,this);
			this.btnu.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.headmove,this);
			this.btnr.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.headmove,this);
			   this.removeChild(this.head);
			   this.removeChild(this.body);
			   this.btnstop.visible=false;
			   this.btnstop.touchEnabled=false;
			   this.btnjs.touchEnabled=false;
			   this.btnu.touchEnabled=false;
			   this.btnl.touchEnabled=false;
			   this.btnd.touchEnabled=false;
			   this.btnr.touchEnabled=false;

			   this.removeChild(this.showscrol);
            
			  this.scrolban=new tcs.scrolpanel();
			  this.scrolban.printscrolpanel(this.scrol);
			  this.addChild(this.scrolban);
		}
		private createanj(){
			this.btnl.addEventListener(egret.TouchEvent.TOUCH_TAP,this.headmove,this)
			this.btnr.addEventListener(egret.TouchEvent.TOUCH_TAP,this.headmove,this)
            this.btnu.addEventListener(egret.TouchEvent.TOUCH_TAP,this.headmove,this)
			this.btnd.addEventListener(egret.TouchEvent.TOUCH_TAP,this.headmove,this)
		}
	}
}